<template>
  <div class="comment">
    <div class="addcomment" v-show="!isFocus">
      <!-- handlerFocus如果没有加（）,默认跟着$event事件对象 -->
      <input type="text" placeholder="写跟帖" @focus="handlerFocus" />
      <span class="comment">
        <i class="iconfont iconpinglun-"></i>
        <em>{{ commentLength }}</em>
      </span>

      <!-- 收藏按钮 -->
      <i
        class="iconfont iconshoucang"
        @click="collectBtn"
        :class="{ star: yellow }"
      ></i>

      <i class="iconfont iconfenxiang"></i>
    </div>
    <!-- 写评论的多行文本框 -->
    <div class="inputcomment" v-show="isFocus">
      <textarea ref="commtext" rows="5" v-model="commentContent"></textarea>
      <div>
        <!-- 给发送点击绑定事件 -->
        <span @click="sendComment">发 送</span>
        <!-- 当点击取消时 isFocus为false -->
        <span @click="isFocus = false">取 消</span>
      </div>
    </div>
  </div>
</template>

<script>
import { publishComment } from "../api/news";
import { collectArticleDetail } from "../api/user";
export default {
  data() {
    return {
      isFocus: false,
      commentContent: "",
      yellow: false,
    };
  },
  // 父传子 儿子用props接收数据
  props: {
    // 评论数量
    commentLength: {
      type: Number,
    },
  },
  methods: {
    // 当底栏输入框获取焦点时，我们要显示多行文本
    handlerFocus(parentId) {
      if (typeof parentId === "number") {
        // 存储回复哪一条评论的id
        this.parentId = parentId;
      } else {
        // 底栏回复评论后再评论，把回复评论id重置为null
        this.parentId = null;
      }
      // 显示多行文本
      this.isFocus = true;
      // 清空上一次的评论内容
      this.commentContent = "";
    },
    // 点击发送时
    sendComment() {
      // 调用接口发布评论
      publishComment(
        this.$route.params.id,
        this.commentContent,
        this.parentId
      ).then((res) => {
        // 给父级发一个事件，要求更新评论列表
        this.$emit("update");
        // 发送完成隐藏评论输入框
        this.isFocus = false;
      });
    },
    // 点击收藏按钮
    collectBtn() {
      collectArticleDetail(this.$route.params.id).then((res) => {
        console.log(res);
        if (res.data.message === "收藏成功") {
          this.$toast("收藏成功");
          this.yellow = true;
        } else {
          this.$toast("取消收藏");
          this.yellow = false;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.inputcomment {
  // 底栏写评论的布局增加定位
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 10px;
  box-sizing: border-box;
  width: 100%;
  display: flex;
  background-color: #fff;
  align-items: flex-end;
  textarea {
    flex: 3;
    background-color: #eee;
    border: none;
    border-radius: 10px;
    padding: 10px;
  }
  div {
    padding: 20px;
  }
  span {
    display: block;
    flex: 1;
    height: 24px;
    line-height: 24px;
    padding: 0 10px;
    background-color: #f00;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    font-size: 13px;
  }
}
.addcomment {
  width: 100%;
  box-sizing: border-box;
  padding: 10px;
  margin-top: 20px;
  display: flex;
  text-align: center;
  position: fixed;
  bottom: 0;
  left: 0;
  background-color: #fff;
  > input {
    flex: 4;
    height: 30px;
    line-height: 30px;
    border-radius: 15px;
    border: none;
    background-color: #eee;
    padding-left: 20px;
    font-size: 14px;
  }
  i {
    font-size: 20px;
  }
  > span {
    flex: 1;
    position: relative;
    > em {
      position: absolute;
      right: 0;
      top: -5px;
      font-size: 10px;
      background-color: #f00;
      color: #fff;
      border-radius: 5px;
      padding: 3px 5px;
    }
  }
  > i {
    flex: 1;
  }
}
.star {
  color: yellow;
}
</style>